/*   1:    */ package com.atomikos.icatch.config;
/*   2:    */ 
/*   3:    */ import com.atomikos.datasource.RecoverableResource;
/*   4:    */ import com.atomikos.icatch.CompositeTransactionManager;
/*   5:    */ import com.atomikos.icatch.ExportingTransactionManager;
/*   6:    */ import com.atomikos.icatch.ImportingTransactionManager;
/*   7:    */ import com.atomikos.icatch.SysException;
/*   8:    */ import com.atomikos.icatch.TSListener;
/*   9:    */ import com.atomikos.icatch.admin.LogAdministrator;
/*  10:    */ import com.atomikos.util.ClassLoadingHelper;
/*  11:    */ import java.io.File;
/*  12:    */ import java.io.IOException;
/*  13:    */ import java.io.InputStream;
/*  14:    */ import java.io.PrintStream;
/*  15:    */ import java.io.Serializable;
/*  16:    */ import java.net.MalformedURLException;
/*  17:    */ import java.net.URL;
/*  18:    */ import java.util.ArrayList;
/*  19:    */ import java.util.Enumeration;
/*  20:    */ import java.util.Iterator;
/*  21:    */ import java.util.List;
/*  22:    */ import java.util.Properties;
/*  23:    */ import java.util.Stack;
/*  24:    */ import javax.transaction.TransactionManager;
/*  25:    */ import javax.transaction.UserTransaction;
/*  26:    */ 
/*  27:    */ public class UserTransactionServiceImp
/*  28:    */   implements Serializable, UserTransactionService
/*  29:    */ {
/*  30:    */   private static final long serialVersionUID = -3374591336514451887L;
/*  31:    */   public static final String NO_FILE_PROPERTY_NAME = "com.atomikos.icatch.no_file";
/*  32:    */   public static final String FILE_PATH_PROPERTY_NAME = "com.atomikos.icatch.file";
/*  33:    */   public static final String HIDE_INIT_FILE_PATH_PROPERTY_NAME = "com.atomikos.icatch.hide_init_file_path";
/*  34:    */   public static final String DEFAULT_PROPERTIES_FILE_NAME = "transactions.properties";
/*  35:    */   private static final String PRE_3_0_DEFAULT_PROPERTIES_FILE_NAME = "jta.properties";
/*  36:    */   private Properties properties_;
/*  37:    */   private List tsListeners_;
/*  38:    */   private List logAdministrators_;
/*  39:    */   private List resources_;
/*  40:    */   private transient UserTransactionService delegate_;
/*  41:    */   
/*  42:    */   private static String evaluateReference(String value, Properties properties)
/*  43:    */   {
/*  44:132 */     String result = value;
/*  45:    */     
/*  46:    */ 
/*  47:135 */     int startIndex = value.indexOf('$');
/*  48:136 */     if ((startIndex > -1) && (value.charAt(startIndex + 1) == '{'))
/*  49:    */     {
/*  50:138 */       int endIndex = value.indexOf('}');
/*  51:139 */       if (startIndex + 2 == endIndex) {
/*  52:140 */         throw new IllegalArgumentException("property ref cannot refer to an empty name: ${}");
/*  53:    */       }
/*  54:141 */       if (endIndex == -1) {
/*  55:142 */         throw new IllegalArgumentException("unclosed property ref: ${" + value.substring(startIndex + 2));
/*  56:    */       }
/*  57:145 */       String subPropertyKey = value.substring(startIndex + 2, endIndex);
/*  58:    */       
/*  59:147 */       String subPropertyValue = properties.getProperty(subPropertyKey);
/*  60:148 */       if (subPropertyValue == null) {
/*  61:150 */         subPropertyValue = System.getProperty(subPropertyKey);
/*  62:    */       }
/*  63:153 */       if (subPropertyValue != null)
/*  64:    */       {
/*  65:155 */         result = result.substring(0, startIndex) + subPropertyValue + result.substring(endIndex + 1);
/*  66:    */         
/*  67:157 */         result = evaluateReference(result, properties);
/*  68:    */       }
/*  69:    */     }
/*  70:166 */     return result;
/*  71:    */   }
/*  72:    */   
/*  73:    */   private static void logToStdErr(String msg)
/*  74:    */   {
/*  75:172 */     if (System.getProperty("com.atomikos.icatch.hide_init_file_path") == null) {
/*  76:173 */       System.err.println(msg);
/*  77:    */     }
/*  78:    */   }
/*  79:    */   
/*  80:    */   private static Properties mergeProperties(Properties first, Properties defaults)
/*  81:    */   {
/*  82:181 */     Enumeration names = first.propertyNames();
/*  83:182 */     while (names.hasMoreElements())
/*  84:    */     {
/*  85:183 */       String name = (String)names.nextElement();
/*  86:184 */       String value = first.getProperty(name);
/*  87:185 */       defaults.setProperty(name, value);
/*  88:    */     }
/*  89:187 */     return defaults;
/*  90:    */   }
/*  91:    */   
/*  92:    */   private URL findPropertiesFileInClasspath(String fileName)
/*  93:    */   {
/*  94:192 */     URL ret = null;
/*  95:    */     
/*  96:    */ 
/*  97:195 */     ret = ClassLoadingHelper.loadResourceFromClasspath(getClass(), fileName);
/*  98:197 */     if (ret == null)
/*  99:    */     {
/* 100:200 */       getClass().getClassLoader();ret = ClassLoader.getSystemResource(fileName);
/* 101:    */     }
/* 102:204 */     return ret;
/* 103:    */   }
/* 104:    */   
/* 105:    */   private Properties findProperties()
/* 106:    */   {
/* 107:209 */     Properties p = new Properties();
/* 108:    */     
/* 109:    */ 
/* 110:    */ 
/* 111:    */ 
/* 112:    */ 
/* 113:    */ 
/* 114:216 */     URL url = null;
/* 115:217 */     if (System.getProperty("com.atomikos.icatch.no_file") == null)
/* 116:    */     {
/* 117:219 */       String filename = System.getProperty("com.atomikos.icatch.file");
/* 118:220 */       if (filename == null)
/* 119:    */       {
/* 120:221 */         filename = "transactions.properties";
/* 121:222 */         logToStdErr("No properties path set - looking for transactions.properties in classpath...");
/* 122:    */         
/* 123:    */ 
/* 124:225 */         url = findPropertiesFileInClasspath(filename);
/* 125:226 */         if (url == null)
/* 126:    */         {
/* 127:227 */           filename = "jta.properties";
/* 128:228 */           logToStdErr("transactions.properties not found - looking for jta.properties in classpath...");
/* 129:    */           
/* 130:    */ 
/* 131:231 */           url = findPropertiesFileInClasspath(filename);
/* 132:    */         }
/* 133:    */       }
/* 134:    */       else
/* 135:    */       {
/* 136:236 */         File file = new File(filename);
/* 137:    */         try
/* 138:    */         {
/* 139:239 */           url = file.toURL();
/* 140:    */         }
/* 141:    */         catch (MalformedURLException e) {}
/* 142:    */       }
/* 143:    */       try
/* 144:    */       {
/* 145:251 */         if (url == null) {
/* 146:251 */           throw new IOException();
/* 147:    */         }
/* 148:252 */         logToStdErr("Using init file: " + url.getPath());
/* 149:253 */         InputStream in = url.openStream();
/* 150:254 */         p.load(in);
/* 151:255 */         in.close();
/* 152:    */       }
/* 153:    */       catch (IOException io)
/* 154:    */       {
/* 155:259 */         String msg = "Failed to open transactions properties file - using default values";
/* 156:    */         
/* 157:261 */         logToStdErr(msg);
/* 158:    */         
/* 159:263 */         p.setProperty("com.atomikos.icatch.service", "com.atomikos.icatch.standalone.UserTransactionServiceFactory");
/* 160:    */       }
/* 161:    */     }
/* 162:    */     else
/* 163:    */     {
/* 164:272 */       p.setProperty("com.atomikos.icatch.service", System.getProperty("com.atomikos.icatch.service"));
/* 165:    */     }
/* 166:277 */     Enumeration allProps = p.propertyNames();
/* 167:278 */     while (allProps.hasMoreElements())
/* 168:    */     {
/* 169:279 */       String key = (String)allProps.nextElement();
/* 170:280 */       String raw = p.getProperty(key);
/* 171:281 */       String value = evaluateReference(raw, p);
/* 172:282 */       if (!raw.equals(value)) {
/* 173:283 */         p.setProperty(key, value);
/* 174:    */       }
/* 175:    */     }
/* 176:287 */     return p;
/* 177:    */   }
/* 178:    */   
/* 179:    */   private String getOrFindProperty(String name)
/* 180:    */   {
/* 181:299 */     if (this.properties_ == null) {
/* 182:299 */       this.properties_ = findProperties();
/* 183:    */     }
/* 184:301 */     return this.properties_.getProperty(name);
/* 185:    */   }
/* 186:    */   
/* 187:    */   public UserTransactionServiceImp()
/* 188:    */   {
/* 189:311 */     this.tsListeners_ = new ArrayList();
/* 190:312 */     this.logAdministrators_ = new ArrayList();
/* 191:313 */     this.resources_ = new ArrayList();
/* 192:    */   }
/* 193:    */   
/* 194:    */   public UserTransactionServiceImp(Properties properties)
/* 195:    */   {
/* 196:328 */     this();
/* 197:    */     
/* 198:330 */     this.properties_ = findProperties();
/* 199:    */     
/* 200:    */ 
/* 201:333 */     this.properties_.putAll(properties);
/* 202:    */   }
/* 203:    */   
/* 204:    */   private void checkInit()
/* 205:    */   {
/* 206:339 */     if (this.delegate_ != null) {
/* 207:339 */       return;
/* 208:    */     }
/* 209:341 */     String factoryClassName = getOrFindProperty("com.atomikos.icatch.service");
/* 210:343 */     if (factoryClassName == null) {
/* 211:344 */       throw new SysException("UserTransactionServiceImp: property not defined: com.atomikos.icatch.service");
/* 212:    */     }
/* 213:    */     try
/* 214:    */     {
/* 215:348 */       Class factoryClass = ClassLoadingHelper.loadClass(factoryClassName);
/* 216:349 */       UserTransactionServiceFactory factory = (UserTransactionServiceFactory)factoryClass.newInstance();
/* 217:    */       
/* 218:351 */       this.delegate_ = factory.getUserTransactionService(this.properties_);
/* 219:    */     }
/* 220:    */     catch (Exception e)
/* 221:    */     {
/* 222:356 */       Stack errors = new Stack();
/* 223:357 */       errors.push(e);
/* 224:358 */       throw new SysException("Error in init of UserTransactionServiceImp: " + e.getMessage(), errors);
/* 225:    */     }
/* 226:    */   }
/* 227:    */   
/* 228:    */   public TSInitInfo createTSInitInfo()
/* 229:    */   {
/* 230:369 */     TSInitInfo ret = null;
/* 231:370 */     checkInit();
/* 232:371 */     ret = this.delegate_.createTSInitInfo();
/* 233:372 */     Properties p = mergeProperties(this.properties_, ret.getProperties());
/* 234:373 */     ret.setProperties(p);
/* 235:374 */     return ret;
/* 236:    */   }
/* 237:    */   
/* 238:    */   public void shutdown(boolean force)
/* 239:    */     throws IllegalStateException
/* 240:    */   {
/* 241:385 */     checkInit();
/* 242:386 */     this.delegate_.shutdown(force);
/* 243:    */   }
/* 244:    */   
/* 245:    */   public void init(TSInitInfo info)
/* 246:    */     throws SysException
/* 247:    */   {
/* 248:397 */     checkInit();
/* 249:398 */     Iterator it = this.resources_.iterator();
/* 250:399 */     while (it.hasNext())
/* 251:    */     {
/* 252:400 */       RecoverableResource nxt = (RecoverableResource)it.next();
/* 253:401 */       registerResource(nxt);
/* 254:    */     }
/* 255:403 */     it = this.logAdministrators_.iterator();
/* 256:404 */     while (it.hasNext())
/* 257:    */     {
/* 258:405 */       LogAdministrator nxt = (LogAdministrator)it.next();
/* 259:406 */       registerLogAdministrator(nxt);
/* 260:    */     }
/* 261:408 */     it = this.tsListeners_.iterator();
/* 262:409 */     while (it.hasNext())
/* 263:    */     {
/* 264:410 */       TSListener nxt = (TSListener)it.next();
/* 265:411 */       registerTSListener(nxt);
/* 266:    */     }
/* 267:413 */     this.delegate_.init(info);
/* 268:    */   }
/* 269:    */   
/* 270:    */   public CompositeTransactionManager getCompositeTransactionManager()
/* 271:    */   {
/* 272:423 */     checkInit();
/* 273:424 */     return this.delegate_.getCompositeTransactionManager();
/* 274:    */   }
/* 275:    */   
/* 276:    */   public TransactionManager getTransactionManager()
/* 277:    */   {
/* 278:434 */     checkInit();
/* 279:435 */     return this.delegate_.getTransactionManager();
/* 280:    */   }
/* 281:    */   
/* 282:    */   public UserTransaction getUserTransaction()
/* 283:    */   {
/* 284:445 */     checkInit();
/* 285:446 */     return this.delegate_.getUserTransaction();
/* 286:    */   }
/* 287:    */   
/* 288:    */   public ImportingTransactionManager getImportingTransactionManager()
/* 289:    */   {
/* 290:455 */     checkInit();
/* 291:456 */     return this.delegate_.getImportingTransactionManager();
/* 292:    */   }
/* 293:    */   
/* 294:    */   public ExportingTransactionManager getExportingTransactionManager()
/* 295:    */   {
/* 296:465 */     checkInit();
/* 297:466 */     return this.delegate_.getExportingTransactionManager();
/* 298:    */   }
/* 299:    */   
/* 300:    */   public TSMetaData getTSMetaData()
/* 301:    */   {
/* 302:476 */     checkInit();
/* 303:477 */     return this.delegate_.getTSMetaData();
/* 304:    */   }
/* 305:    */   
/* 306:    */   public void registerResource(RecoverableResource res)
/* 307:    */   {
/* 308:485 */     checkInit();
/* 309:486 */     this.delegate_.registerResource(res);
/* 310:    */   }
/* 311:    */   
/* 312:    */   public void registerLogAdministrator(LogAdministrator admin)
/* 313:    */   {
/* 314:495 */     checkInit();
/* 315:496 */     this.delegate_.registerLogAdministrator(admin);
/* 316:    */   }
/* 317:    */   
/* 318:    */   public Enumeration getResources()
/* 319:    */   {
/* 320:505 */     checkInit();
/* 321:506 */     return this.delegate_.getResources();
/* 322:    */   }
/* 323:    */   
/* 324:    */   public Enumeration getLogAdministrators()
/* 325:    */   {
/* 326:514 */     checkInit();
/* 327:515 */     return this.delegate_.getLogAdministrators();
/* 328:    */   }
/* 329:    */   
/* 330:    */   public void removeResource(RecoverableResource res)
/* 331:    */   {
/* 332:520 */     checkInit();
/* 333:521 */     this.delegate_.removeResource(res);
/* 334:    */   }
/* 335:    */   
/* 336:    */   public void removeLogAdministrator(LogAdministrator admin)
/* 337:    */   {
/* 338:527 */     checkInit();
/* 339:528 */     this.delegate_.removeLogAdministrator(admin);
/* 340:    */   }
/* 341:    */   
/* 342:    */   public void registerTSListener(TSListener listener)
/* 343:    */   {
/* 344:533 */     checkInit();
/* 345:534 */     this.delegate_.registerTSListener(listener);
/* 346:    */   }
/* 347:    */   
/* 348:    */   public void removeTSListener(TSListener listener)
/* 349:    */   {
/* 350:539 */     checkInit();
/* 351:540 */     this.delegate_.removeTSListener(listener);
/* 352:    */   }
/* 353:    */   
/* 354:    */   public void shutdownForce()
/* 355:    */   {
/* 356:551 */     shutdown(true);
/* 357:    */   }
/* 358:    */   
/* 359:    */   public void shutdownWait()
/* 360:    */   {
/* 361:562 */     shutdown(false);
/* 362:    */   }
/* 363:    */   
/* 364:    */   public void setInitialRecoverableResources(List resources)
/* 365:    */   {
/* 366:573 */     this.resources_ = resources;
/* 367:    */   }
/* 368:    */   
/* 369:    */   public void setInitialLogAdministrators(List administrators)
/* 370:    */   {
/* 371:584 */     this.logAdministrators_ = administrators;
/* 372:    */   }
/* 373:    */   
/* 374:    */   public void setInitialTSListeners(List listeners)
/* 375:    */   {
/* 376:594 */     this.tsListeners_ = listeners;
/* 377:    */   }
/* 378:    */   
/* 379:    */   public void init()
/* 380:    */   {
/* 381:604 */     TSInitInfo info = createTSInitInfo();
/* 382:605 */     init(info);
/* 383:    */   }
/* 384:    */   
/* 385:    */   public void init(Properties properties)
/* 386:    */     throws SysException
/* 387:    */   {
/* 388:611 */     TSInitInfo info = createTSInitInfo();
/* 389:612 */     mergeProperties(properties, info.getProperties());
/* 390:613 */     init(info);
/* 391:    */   }
/* 392:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions-api\3.7.0\transactions-api-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.config.UserTransactionServiceImp
 * JD-Core Version:    0.7.0.1
 */